const path = require('path')
const RxFileWrite = require('rxfile-write')
const { getHtml } = require('./get-html')

const htmlArray = getHtml()

let htmlCodeString = ''
if (htmlArray && Array.isArray(htmlArray) && htmlArray.length) {
  for (let obj of htmlArray) {
    const htmlPath = obj.path
    const htmlName = obj.name
    console.log('htmlArray -> obj=>', obj)
    const div = `
    <div class="html">
      <a href="${htmlPath}">${htmlName}</a>
    </div>`
    htmlCodeString += '\n'+ div
  }
  htmlCodeString += '\n'
}

const templatePath = path.resolve(__dirname, '..','index.html')
const srcPath = path.resolve(__dirname, '..', 'src', 'index.html')
RxFileWrite.read(templatePath).then(data => {
  data = data || ''
  const temp = data.toString()
  const newData = temp.replace(
    '<!-- 模板 -->',
    htmlCodeString
  )
  
  RxFileWrite.writeSync(srcPath, newData)
})
